package com.css.dzj.dzjap.modules.record.approject.contorller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.annotation.SysLog;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.css.dzj.dzjap.modules.record.approject.entity.ApRecordEntity;
import com.css.dzj.dzjap.modules.record.approject.service.ApRecordService;
import com.css.dzj.dzjap.modules.todolist.service.ApTodoService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 安评项目备案信息表增删改查
 *
 * @author zhouchanghe
 */
@RestController
@RequestMapping("/aprecord")
public class ApRecordController {

    @Autowired
    private ApRecordService apRecordService;
    @Autowired
    private ApTodoService apTodoService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
    //多条件查询：项目名称、工程类别、状态、备案人员、备案申请开始时间、备案申请结束时间等
    @GetMapping("/findPage")
    public R getPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                     @RequestParam(name = "userName", required = false) String userName,
                     @RequestParam(name = "recordProjectName", required = false) String recordProjectName,
                     @RequestParam(name = "recordProjectType", required = false) String recordProjectType,
                     @RequestParam(name = "recordUser", required = false) String recordUser,
                     @RequestParam(name = "status", required = false) String status,
                     @RequestParam(name = "recordPersonName", required = false) String recordPersonName,
                     @RequestParam(name = "recordTimeStart", required = false) String recordTimeStart,
                     @RequestParam(name = "recordTimeEnd", required = false) String recordTimeEnd,
                     @RequestParam(name = "recordProjectAddress", required = false) String recordProjectAddress,
                     @RequestParam(name = "reportStatus", required = false) String reportStatus) {
        String s = "";
        try {
            if (StringUtils.isEmpty(recordProjectAddress)) {
                return R.error("该用户没有权限");
            }
            PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
            QueryResults<ApRecordEntity> page = apRecordService.getPage(userName, recordProjectName, recordProjectType, status, recordUser, recordTimeStart, recordTimeEnd, recordProjectAddress, reportStatus, pageRequest);
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("多条件查询备案信息成功", Constant.APRECORD, Constant.PT, Constant.PAGE, s, request);
            return R.ok().put("data", page);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("多条件查询备案信息失败", Constant.APRECORD, Constant.PT, Constant.PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    @SysLog("获取安评项目备案详情信息")
    @PostMapping("/getAprecord")
    public R ApRecordById(@RequestParam(name = "id") String id) {
        String s = "";
        try{
            ApRecordEntity ApRecordEntity = apRecordService.queryApRecordById(id);
            s = JSON.toJSONString(ApRecordEntity);
            sysAuditService.saveSuccessAudit("安评项目备案查看详情成功", Constant.APRECORD, Constant.PT, Constant.GETAPRECORD, s, request);
            return R.ok().put("data", ApRecordEntity);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("安评项目备案查看详情失败", Constant.APRECORD, Constant.PT, Constant.GETAPRECORD, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 安评项目备案添加
     *
     * @param apRecordEntity
     * @return
     */
    @SysLog("创建安评项目备案")
    @PostMapping("/add")
    @Transactional
    public R add(@RequestBody ApRecordEntity apRecordEntity) {
        String s = "";
        try {
            apRecordService.save(apRecordEntity);
            s = JSON.toJSONString(apRecordEntity);
            sysAuditService.saveSuccessAudit("创建安评项目备案新增成功", Constant.APRECORD, Constant.PT, Constant.ADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("创建安评项目备案新增失败", Constant.APRECORD, Constant.PT, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 通过id删除安评项目备案
     *
     * @param id
     * @return
     */
    @SysLog("逻辑删除安评项目备案")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        String s = "";
        try {
            ApRecordEntity apRecordEntity = apRecordService.queryApRecordById(id);
            if (apRecordEntity == null) {
                return R.error("安评项目备案信息不存在");
            }
            apRecordEntity.setDelFlag(Constant.DELETE);
            apRecordService.update(apRecordEntity);
            s = JSON.toJSONString(apRecordEntity);
            sysAuditService.saveSuccessAudit("安评项目备案信息删除成功", Constant.APRECORD, Constant.PT, Constant.DEL, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("安评项目备案信息删除失败", Constant.APRECORD, Constant.PT, Constant.DEL, s, e, request);
            return R.error(e.getMessage());
        }
    }

    @SysLog("修改安评项目备案")
    @PostMapping("/update/{id}")
    public R update(@PathVariable String id, @RequestBody ApRecordEntity apRecordEntity) {
        String s = "";
        try {
            ApRecordEntity targetApRecordEntity = apRecordService.queryApRecordById(id);
            BeanUtil.copyBeanNotNullToBean(apRecordEntity, targetApRecordEntity);
            targetApRecordEntity.setUpdateTime(new Date());
            if(targetApRecordEntity.getStatus().equals("2")){
                targetApRecordEntity.setStatus("0");
            }
            apRecordService.update(targetApRecordEntity);
            s = JSON.toJSONString(targetApRecordEntity);
            sysAuditService.saveSuccessAudit("安评项目备案信息修改成功", Constant.APRECORD, Constant.PT, Constant.UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("安评项目备案信息修改失败", Constant.APRECORD, Constant.PT, Constant.UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 安评项目备案暂存
     *
     * @param apRecordEntity * @return
     */
    @SysLog("暂存安评项目备案")
    @PostMapping("/zcadd")
    public R zcadd(@RequestBody ApRecordEntity apRecordEntity) {
        String s = "";
        try {
            apRecordService.zcsave(apRecordEntity);
            s = JSON.toJSONString(apRecordEntity);
            sysAuditService.saveSuccessAudit("安评项目备案信息暂存成功", Constant.APRECORD, Constant.PT, Constant.ZCADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("安评项目备案信息暂存失败", Constant.APRECORD, Constant.PT, Constant.ZCADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    @SysLog("审批安评项目备案")
    @PostMapping("/check/{id}")
    public R check(@PathVariable String id, @RequestBody ApRecordEntity apRecordEntity) {
        String s = "";
        try {
            ApRecordEntity targetApRecordEntity = apRecordService.queryApRecordById(id);
            BeanUtil.copyBeanNotNullToBean(apRecordEntity, targetApRecordEntity);
            //审核结果不通过
            if (apRecordEntity.getCheckResult().equals(Constant.CHECK_RESULT_NO)) {
                targetApRecordEntity.setStatus(Constant.CHECK_STATUS2);
            }
            //审核结果通过
            if (apRecordEntity.getCheckResult().equals(Constant.CHECK_RESULT_YES)) {
                targetApRecordEntity.setStatus(Constant.CHECK_STATUS3);
            }
            targetApRecordEntity.setUpdateTime(new Date());
            apRecordService.check(targetApRecordEntity);
            s = JSON.toJSONString(targetApRecordEntity);
            sysAuditService.saveSuccessAudit("安评项目备案信息审批成功", Constant.APRECORD, Constant.PT, Constant.CHECK, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("安评项目备案信息审批失败", Constant.APRECORD, Constant.PT, Constant.CHECK, s, e, request);
            return R.error(e.getMessage());
        }
    }

    @SysLog("撤回安评项目备案")
    @PostMapping("/cancel/{id}")
    public R cancel(@PathVariable String id, @RequestBody ApRecordEntity apRecordEntity) {
        String s = "";
        try {
            ApRecordEntity targetApRecordEntity = apRecordService.queryApRecordById(id);
            //未提交（已撤回）
            targetApRecordEntity.setStatus(Constant.CHECK_STATUS0);
            targetApRecordEntity.setUpdateTime(new Date());
            apRecordService.cancel(targetApRecordEntity);
            s = JSON.toJSONString(targetApRecordEntity);
            sysAuditService.saveSuccessAudit("安评项目备案信息撤回成功", Constant.APRECORD, Constant.PT, Constant.CANCEL, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("安评项目备案信息撤回失败", Constant.APRECORD, Constant.PT,Constant.CANCEL, s, e, request);
            return R.error(e.getMessage());
        }
    }

    @SysLog("上报安评项目备案")
    @PostMapping("/report/{id}")
    public R report(@PathVariable String id, @RequestBody ApRecordEntity apRecordEntity) {
        String s = "";
        try {
            ApRecordEntity targetApRecordEntity = apRecordService.queryApRecordById(id);
            //已上报
            targetApRecordEntity.setReportStatus(Constant.REPORT_STATUS_YES);
            targetApRecordEntity.setUpdateTime(new Date());
            apRecordService.update(targetApRecordEntity);
            s = JSON.toJSONString(targetApRecordEntity);
            sysAuditService.saveSuccessAudit("安评项目备案信息上报成功", Constant.APRECORD, Constant.PT, Constant.REPORT, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("安评项目备案信息上报失败", Constant.APRECORD, Constant.PT,Constant.REPORT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    @SysLog("提交安评项目备案")
    @PostMapping("/submit/{id}")
    public R submit(@PathVariable String id, @RequestBody ApRecordEntity apRecordEntity) {
        String s="";
        try {
            ApRecordEntity targetApRecordEntity = apRecordService.queryApRecordById(id);
            BeanUtil.copyBeanNotNullToBean(apRecordEntity, targetApRecordEntity);
            //已提交待审批
            targetApRecordEntity.setStatus(Constant.CHECK_STATUS1);
            targetApRecordEntity.setUpdateTime(new Date());
            apRecordService.submit(targetApRecordEntity);
            s = JSON.toJSONString(targetApRecordEntity);
            sysAuditService.saveSuccessAudit("提交安评项目备案成功", Constant.APRECORD, Constant.PT, Constant.SUBMIT, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("提交安评项目备案失败", Constant.APRECORD, Constant.PT ,Constant.SUBMIT, s, e, request);
            return R.error(e.getMessage());
        }
    }
    @SysLog("查询安评项目涉核和国家水电工程备案")
    @GetMapping("/findShPage")
    public R getShPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                       @RequestParam(name = "recordProjectName", required = false) String recordProjectName,
                       @RequestParam(name = "recordProjectType", required = false) String recordProjectType,
                       @RequestParam(name = "recordUser", required = false) String recordUser,
                       @RequestParam(name = "status", required = false) String status,
                       @RequestParam(name = "recordPersonName", required = false) String recordPersonName,
                       @RequestParam(name = "recordTimeStart", required = false) String recordTimeStart,
                       @RequestParam(name = "recordTimeEnd", required = false) String recordTimeEnd,
                       @RequestParam(name = "recordProjectAddress", required = false) String recordProjectAddress,
                       @RequestParam(name = "reportStatus", required = false) String reportStatus
                     ) {
        String  s  = "";
        try {
            if (StringUtils.isEmpty(recordProjectAddress)) {
                return R.error("该用户没有权限");
            }
            PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
            QueryResults<ApRecordEntity> page = apRecordService.getShPage(recordProjectName, recordProjectType, status, recordUser, recordTimeStart, recordTimeEnd, recordProjectAddress, reportStatus, pageRequest);
            s = JSON.toJSONString(page);
            sysAuditService.saveSuccessAudit("查询安评项目涉核和国家水电工程备案成功", Constant.APRECORD,Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", page);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询安评项目涉核和国家水电工程备案成功失败", Constant.APRECORD, Constant.PT, Constant.PAGE, s, e, request);
            return R.error(e.getMessage());
        }
    }
    @SysLog("修改抽取状态")
    @PostMapping("/updatestatus")
    public R updatestatus(@RequestParam(name = "id") String id) {
        String s = "";
        try {
            ApRecordEntity targetApRecordEntity = apRecordService.queryApRecordById(id);
            //已抽取
            targetApRecordEntity.setRecordExtractStauts(Constant.EXTRACT);
            targetApRecordEntity.setUpdateTime(new Date());
            apRecordService.update(targetApRecordEntity);
            s = JSON.toJSONString(targetApRecordEntity);
            sysAuditService.saveSuccessAudit("修改抽取状态成功", Constant.APRECORD, Constant.PT, Constant.UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("修改抽取状态失败", Constant.APRECORD, Constant.PT,Constant.UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }
    @SysLog("拟办功能添加通知公告")
    @PostMapping("/addHandle")
    public R addHandle(@RequestBody ApPublicEntity pub){
        String s = "";
        try {
            apRecordService.addHandle(pub);
            s = JSON.toJSONString(pub);
            sysAuditService.saveSuccessAudit("拟办功能添加通知公告成功", Constant.APRECORD, Constant.PT, Constant.ADD, s, request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("拟办功能添加通知公告失败", Constant.APRECORD, Constant.PT,Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 获取项目备案表数据信息
     */
    @GetMapping("/queryAllRecordMsg")
    public List queryAllJcdx() {
        String s = "";
        try {
            List<ApRecordEntity> list = apRecordService.queryAllRecord();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
